// 查询服务分类
const axios = require("axios");
const query = {
  row: (id) => `db.collection('serve').where({_id:"${id}"}).get()`,
  get: (params) => {
    return `db.collection('serve').where({
            parentId: ${params.parentId ? params.parentId : "_.exists(true)"},
            name: /${params.name || "."}/,
            version: ${params.version || "_.exists(false)"},
        }).orderBy('order', 'id', 'asc').limit(
            ${params.pageSize || 10}).skip(${
      params.pageSize * (params.pageNo - 1) || 0
    }
        ).get()`;
  },
  add: (row) => `db.collection("serve").add({
        data: ${JSON.stringify(row)}
      })`,
  del: (id) => 'db.collection("serve").where({_id:"' + id + '"}).remove()',
  update: (id, row) => `db.collection("serve").where({_id:"${id}"}).update({
        data: ${JSON.stringify(row)}
    })`,
};
const getInfo = (access_token, id) => {
  return axios({
    method: "POST",
    url: `/tcb/databasequery?access_token=${access_token}`,
    data: {
      env: "watab-0g38tjg3f9db387b",
      query: query.row(id),
    },
  });
};
const get = (access_token, params) => {
  return axios({
    method: "POST",
    url: `/tcb/databasequery?access_token=${access_token}`,
    data: {
      env: "watab-0g38tjg3f9db387b",
      query: query.get(params),
    },
  });
};
const del = (access_token, id) => {
  return axios({
    method: "POST",
    url: `/tcb/databasedelete?access_token=${access_token}`,
    data: {
      env: "watab-0g38tjg3f9db387b",
      query: query.del(id),
    },
  });
};
const add = (access_token, row) => {
  return axios({
    method: "POST",
    url: `/tcb/databaseadd?access_token=${access_token}`,
    data: {
      env: "watab-0g38tjg3f9db387b",
      query: query.add(row),
    },
  });
};
const update = (access_token, row) => {
  const new_row = {
    ...row,
  };
  const { _id } = new_row;
  delete row._id;
  return axios({
    method: "POST",
    url: `/tcb/databaseupdate?access_token=${access_token}`,
    data: {
      env: "watab-0g38tjg3f9db387b",
      query: query.update(_id, new_row),
    },
  });
};

module.exports = {
  getInfo,
  get,
  add,
  del,
  update,
};
